<?php
include_once("includes/common_public_inc.php");
$pagename=LA_NEWSLETTER;
/*Parametros*/
$mercadoVial 	   = getParamInt("vial",0);
$mercadoTransporte = getParamInt("trans",0);
$mercadoGruas 	   = getParamInt("grua",0);
$mercadoAgricola   = getParamInt("agro",0);
$mercadoServicios  = getParamInt("serv",0);
$limit=getParamInt("limit","");
if(empty($limit)) $limit = 15;
$diasAntes=getParamInt("days","");
/**Base URL ***/
$baseUrl = "http://".$set_url;
/* re armado de la URL */
$urlCurrent = $baseUrl.'/newsletter.php?vial='.$mercadoVial.'&trans='.$mercadoTransporte.'&grua='.$mercadoGruas.'&agro='.$mercadoAgricola.'&serv='.$mercadoServicios.'&limit='.$limit;
if(!empty($diasAntes))
	$urlCurrent .= '&days='.$diasAntes;
/** fin Obtencion parametros **/
$mercadosArray = array(
	"Venta Máquinas Viales Equipos"  => $mercadoVial,
	"Venta Camiones & Transportes" => $mercadoTransporte,
	"Venta Grúas Autoelevadores"  => $mercadoGruas,
	"Venta Maquinaria Agrícola & Tractores"  => $mercadoAgricola,
	"Servicios" => $mercadoServicios
);

//Info de mercado, imagenes de catageorias, nombres y IDs
$arrayOfMercados = getArrayOfCatImages();
/*** inicializacion del template **/
$tplList = new TplLoad;


$granArrayDeArrays = array(); //tendra toda la información del Newsletter
$i = 0;
$totalOfAds = 0;
foreach ($mercadosArray as $mercadoname => $value){

	if($value==0) continue;
	$i++;
	$granArrayDeArrays[$i] = array();	
	//Obtenemos el(los) ids de ese mercado
	$categoryIdArray = array(); //Array de Ids, de mercados que quiero mostrar
	
	foreach ($arrayOfMercados as $mercadoID => $values){		
		if($values['cat_name'] == $mercadoname){
			$categoryIdArray[]=$mercadoID;
		}
	}
	
	//resultados
	$resultados = getAds4Newsletter($categoryIdArray,$limit,$diasAntes); //Obtengo dichos anuncios
	$num_ads =  mysql_num_rows($resultados);
	$totalOfAds += $num_ads;
	$granArrayDeArrays[$i]["NUM_ADS"] = $num_ads;
	$granArrayDeArrays[$i]["CATIMAGE"] = "/catimages/".$arrayOfMercados[$categoryIdArray[0]]['cat_image'];
	$granArrayDeArrays[$i]["CATNAME"]  =  str_replace("Venta", "", $arrayOfMercados[$categoryIdArray[0]]['cat_name']);
	if($categoryIdArray[0] == ID_CAT_SERVICIOS)
		$granArrayDeArrays[$i]["SERVICIOS"]  = 1;
	else
		$granArrayDeArrays[$i]["SERVICIOS"]  = 0;

	$fArray = array();
	while ($row=mysql_fetch_array($resultados))
	{
		$ad_id = $row['ad_id'];
		$ad_has_picture = $row["ad_has_picture"];
		$ad_is_sold = $row["ad_is_sold"];
		$img_stored = $row["img_stored"];
		$ad_description = $row["ad_description"];
		
		$fArray[$ad_id] = array();
		$fArray[$ad_id]["CATEGORYNAME"] 	 =  $row["cat_name"];
		$fArray[$ad_id]["ad_cat_id"]	     =  $row["ad_cat_id"];
		$fArray[$ad_id]["cat_fatherid"] 	 =  $row["cat_fatherid"];
		$fArray[$ad_id]["cat_grandfatherid"] =  $row["cat_grandfatherid"];
		$fArray[$ad_id]["OPERACION"] 		 =  getMainTypeName($row["cat_grandfatherid"]);
		$fArray[$ad_id]["CATIMAGE"] 		 =  $row["cat_image"];	
		$fArray[$ad_id]["DATEADDED"] 		 =  formatDateShort($row["ad_date"]);
		$fArray[$ad_id]["EXPIREDATE"] 		 =  formatDateShort($row["ad_date_expire"]);	
		$fArray[$ad_id]["VIEWED"] 			 =  $row["ad_views"];
		$fArray[$ad_id]["SPECIAL"] 			 =  $row["ad_is_special"];	
		$fArray[$ad_id]["LINK"] 			 =  $baseUrl."/".linkDetail($ad_id);
		$fArray[$ad_id]["TITLE"] 			 =  $row["ad_title"];
		$fArray[$ad_id]["REGDATE"] 			 =  formatDateShort($row["ad_date"]);	
		$fArray[$ad_id]["VENDOR_URL"] 	 	 =  $row["user_vendor_url"];
		$fArray[$ad_id]["USER_NAME"] 		 =  $row["user_name"];
		$fArray[$ad_id]["TIPO"] 		     =  $row["tipo"];
		if($row["pname"]=='EF_GIF')
			$fArray[$ad_id]["pname"] 		 = EF_GIF;
		else
			$fArray[$ad_id]["pname"] 		 = $row["pname"];
		$fArray[$ad_id]["pid"] 				 = $row["mv_package_id"];	
		$fArray[$ad_id]["ad_id"] 			 = $ad_id;	
		$fArray[$ad_id]["DESCRIPTION"] 		 = $ad_description;
		$fArray[$ad_id]["PRECIO"] 			 = $row["ad_precio"];
		$fArray[$ad_id]["MONEDA"] 			 = $row["ad_moneda"];
		$fArray[$ad_id]["ad_enabled"] 		 = $row["ad_enabled"];
		$fArray[$ad_id]["PROVINCIA"] 		 = $row["nombre"];
		$fArray[$ad_id]["MARCA"] 			 = $row["marca"];	
		$fArray[$ad_id]["MODELO"] 			 = $row["mv_modelo"];
		$fArray[$ad_id]["POTENCIA"] 		 = $row["mv_potencia"];
		$fArray[$ad_id]["HSKMS"] 		     = $row["mv_hskms"];
		$fArray[$ad_id]["PACKAGE_ID"] 		 = $row["mv_package_id"];
		$fArray[$ad_id]["ANIO"]				 = $row["mv_anio"];
		$fArray[$ad_id]["LOCALIDAD"] 		 = $row["localidad"];
		
		/*Pasamos la imagen
		 * */
		if ($ad_has_picture)
		{
				// Use real images where possible!
				$file = getOneImage($ad_id,"","","");			 
				$lImage=returnThumbnailFromImageFile($file);
				$fArray[$ad_id]["IMAGEYESNO"] = "<a href='".$baseUrl."/".linkDetail($ad_id)."' style='color:#C23A27;font-size:11px;text-decoration:none;' title='".strEnc(dotString($ad_description,50)) . "'>";
				$fArray[$ad_id]["IMAGEYESNO"].= "<img  border='2' style='color:#CCCCCC;border-color:#CCCCCC;' src='".$baseUrl."/images/$lImage'";
				if ($set_image_program==0)
				{
					$lSizeArray=setImageSize($lImage,getManualSize("thumb"),0);
					$fArray[$ad_id]["IMAGEYESNO"].= " width='".$lSizeArray[0]."' height='".$lSizeArray[1]."'";
				}
				$fArray[$ad_id]["IMAGEYESNO"].= " border='0' alt='".LA_VIEW_AD."' title='".LA_VIEW_AD."' /></a>";
				
				
		}
		else 
		{
				$fArray[$ad_id]["IMAGEYESNO"] = "<a href='".$baseUrl."/".linkDetail($ad_id)."' style='color:#C23A27;font-size:11px;text-decoration:none;'>";
				$fArray[$ad_id]["IMAGEYESNO"].= "<img src='".$baseUrl."/layout_images/no_image_camera.gif' border='2' style='color:#CCCCCC;border-color:#CCCCCC;' alt='".LA_VIEW_AD."' title='".LA_VIEW_AD."' /></a>";
		}	
		/****** END IMAGE **/	
	}//end while
	$granArrayDeArrays[$i]['ads'] = $fArray;  
} //END for each

/*¿mostramos los banners exclusivos del newsletter?*/
if(existBannerSectionLike("newsletter"))
	$tplList->assign('showBanners',1);
	
/**position**/
if($totalOfAds>10)
	$tplList->assign('bannerposition','position: absolute;');
					
					
	
/**Noticias **/
$contentNews = file_get_contents('admin/'.NEWSLETTER_NEWS_FILE);
if(!empty($contentNews))
	$tplList->assign('contentNews',$contentNews);
	
$tplList->assign('urlBase',$baseUrl);
$tplList->assign('urlCurrent',$urlCurrent);
$tplList->assign('totalOfAds',$totalOfAds);
$tplList->assign("mercados",$granArrayDeArrays);
$tplList->display("newsletter.tpl");
?>